import axios from 'axios'
import { Message, Loading } from 'element-ui'
const BASE_URL = 'http://admin-api.macrozheng.com'
let loadingInstance = null

export const request = axios.create({
  baseURL: BASE_URL,
  method: 'GET',
  header: {
    'Content-Type': 'application/json;charset=UTF-8',
  },
})

// 拦截请求
request.interceptors.request.use((config) => {
  loadingInstance = Loading.service({
    lock: false,
    text: 'loading...',
    spinner: 'el-icon-loading',
    background: 'rgba(0, 0, 0, 0.7)',
  })
  return config
})

// 拦截响应
request.interceptors.response.use(
  (response) => {
    loadingInstance.close()
    // console.log(response)
    return response.data
  },
  (error) => {
    console.log('TCL: error', error)
    const msg = error.Message !== undefined ? error.Message : ''
    Message({
      message: '网络错误' + msg,
      type: 'error',
      duration: 3 * 1000,
    })
    loadingInstance.close()
    return Promise.reject(error)
  }
)
